﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2009
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports System.IO

Imports Boogaloo.Core.BLL
Imports Boogaloo.Core.UI

Namespace UI.Webpages

    Partial Public Class LayoutEditor
        Inherits BaseAdminPage


        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            hyp_cancel.NavigateUrl = String.Format("{0}Layouts.aspx", Groove.path_admin_host)

            If Not Page.IsPostBack Then

                ddl_child_layout.DataSource = Groove.admin_root.layouts()
                ddl_child_layout.DataTextField = "menu_item_text"
                ddl_child_layout.DataValueField = "node_id"
                ddl_child_layout.DataBind()

                If ddl_child_layout.Items.Count = 0 Then
                    lbl_ddl_child_layout.Visible = False
                End If
                ddl_child_sort.DataSource = ChildNodeSort.GetChildNodeSortOrders()
                ddl_child_sort.DataTextField = "name"
                ddl_child_sort.DataValueField = "child_sort_type_id"
                ddl_child_sort.DataBind()
                ddl_child_sort.SelectedValue = ChildSortType.ManualNodesInsertedLast


                Dim components As List(Of Component) = Component.GetComponentsManageNode(Groove.admin_root.root_id)
                If components.Count <= 1 Then
                    lbl_ddl_component.Visible = False
                    lbl_ddl_child_component.Visible = False
                Else
                    lbl_ddl_component.Visible = True
                    ddl_component.DataSource = components
                    ddl_component.DataTextField = "name"
                    ddl_component.DataValueField = "component_id"
                    ddl_component.DataBind()

                    lbl_ddl_child_component.Visible = True
                    ddl_child_component.DataSource = components
                    ddl_child_component.DataTextField = "name"
                    ddl_child_component.DataValueField = "component_id"
                    ddl_child_component.DataBind()
                End If

                ddl_mastertemplate.DataSource = Groove.admin_root.master_templates()
                ddl_mastertemplate.DataTextField = "name"
                ddl_mastertemplate.DataValueField = "master_template_id"
                ddl_mastertemplate.DataBind()

                ddl_theme.DataSource = Groove.admin_root.themes
                ddl_theme.DataTextField = "name"
                ddl_theme.DataValueField = "theme_id"
                ddl_theme.DataBind()
                If ddl_theme.Items.Count = 0 Then
                    lbl_ddl_theme.Visible = False
                Else
                    ddl_theme.Items.Insert(0, New ListItem("No ASP.NET Theme", 0))
                End If

                If Groove.admin_root.HasNodeCreationRequirements() And Not Groove.admin_root.HasLayoutNodes() Then
                    chk_root_layout.Checked = True
                    chk_root_layout.Enabled = False
                End If

                BindPage()

            End If

        End Sub

        Private Sub BindPage()
            Dim n As Node = Groove.admin_current_node

            If n IsNot Nothing Then
                h3cph.Visible = True
                hyp_cancel.Visible = True

                txt_name.Text = n.menu_item_text
                chk_root_layout.Checked = (n.root.child_layout_node_id = n.node_id)
                If chk_root_layout.Checked Then chk_root_layout.Enabled = False

                ' don't let the user uncheck this value if there is only one layout
                If Groove.admin_root.layouts.Count = 1 Then chk_root_layout.Enabled = False
                ddl_mastertemplate.SelectedValue = n.master_template_id.ToString
                ddl_theme.SelectedValue = n.theme_id.ToString
                ddl_child_component.SelectedValue = n.child_component_id
                ddl_child_sort.SelectedValue = n.child_sort_type_id
                ddl_child_layout.SelectedValue = n.child_layout_node_id.ToString

            Else
                h3cph.Visible = False

            End If

        End Sub


        Protected Sub lb_save_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_save.Click
            Dim current As Node = Groove.admin_current_node

            Dim component_id As Integer = 0
            Dim show_components As Boolean = Not Component.GetComponentsManageNode(Groove.admin_root.root_id).Count <= 1
            Dim child_component_id As Integer = CType(Boogaloo.Base.Globals.Settings.Components("Core").component_id, Integer)
            If Not show_components Then
                child_component_id = component_id
            Else
                component_id = CType(ddl_component.SelectedValue, Integer)
                child_component_id = CType(ddl_child_component.SelectedValue, Integer)
            End If

            Dim theme_id As Integer = 0
            If Groove.admin_root.themes.Count > 0 Then
                theme_id = CType(ddl_theme.SelectedValue, Integer)
            End If


            If current Is Nothing Then ' insert a new layout

                Dim CREATE_DEFAULT_PAGE As Boolean = Groove.admin_root.HasNodeCreationRequirements() And Not Groove.admin_root.HasLayoutNodes()

                ' insert the layout
                Dim node_id As Integer = Node.InsertNode(0, Groove.admin_root.root_id, NodeType.WebPage, component_id, CType(ddl_mastertemplate.SelectedValue, Integer), Nothing, _
                        child_component_id, CType(ddl_child_sort.SelectedValue, Integer), theme_id, txt_name.Text, _
                        Nothing, txt_name.Text, Nothing, Nothing, Nothing, Nothing, String.Format("Layout: {0}", txt_name.Text), Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, _
                        Nothing, Nothing, Nothing, False, True, True, False, True, False, Nothing)

                If CREATE_DEFAULT_PAGE Then
                    ' special case in which this host does not have a default page or layout to spawn additional pages

                    ' Create the default homepage
                    Dim default_node_id As Integer = Node.InsertNode(0, Groove.admin_root.root_id, NodeType.WebPage, component_id, CType(ddl_mastertemplate.SelectedValue, Integer), node_id, _
                        child_component_id, CType(ddl_child_sort.SelectedValue, Integer), theme_id, "Home", Nothing, _
                        "Home", Nothing, Nothing, Nothing, Nothing, "Home", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, DateTime.Today, Nothing, _
                        Nothing, Nothing, True, True, True, False, False, False, String.Empty)

                    ' Create the default publish record
                    Dim publish_id As Integer = Publish.InsertPublish(Groove.admin_root.root_id, DateTime.Now, "c")

                    Dim r As Root = Groove.admin_root
                    r.child_layout_node_id = node_id
                    r.default_node_id = default_node_id
                    r.Update()

                End If

                current = Node.GetNode(node_id)

            Else ' update an existing layout

                current.menu_item_text = txt_name.Text
                current.page_link_text = txt_name.Text
                current.page_title = String.Format("Layout: {0}", txt_name.Text)

                current.master_template_id = CType(ddl_mastertemplate.SelectedValue, Integer)
                
                If show_components Then current.component_id = CType(ddl_component.SelectedValue, Integer)

                current.child_sort_type_id = CType(ddl_child_sort.SelectedValue, Integer)

                If show_components Then current.child_component_id = CType(ddl_child_component.SelectedValue, Integer)

                current.child_layout_node_id = CType(ddl_child_layout.SelectedValue, Integer)
                
                current.theme_id = theme_id

                current.Update()

            End If

            ' set the root layout if needed
            If chk_root_layout.Checked Then
                Dim r As Root = Groove.admin_root
                r.child_layout_node_id = current.node_id
                r.Update()
            End If

            Response.Redirect(String.Format("{0}LayoutEditor.aspx?node={1}", Groove.path_admin_host, current.node_id))

        End Sub

    End Class

End Namespace
